<template>
    <div class="repositoryPage flex_column flex">
        <div class="titleVue flex_between align_center">
            <div class="title">知识库列表</div>
            <div class="flex align_center">
                <span class="what" @click="dialogTableVisible10=true">什么是知识库?</span>
                <div class="createBtn" @click="dialogTableVisible=true">创建</div>
            </div> 
        </div>

        
        <div class="scrollContent flex_1">
            <!-- 知识库列表 -->
            <el-table :data="list" height="100%" border width="100%" class="center-table">
                <!-- 列表为空 -->
                <div slot="empty" class="flex_column align_center">
                    <img src="../../assets/empty.png" slot="empty" style="width: 313px; height: 172px; margin-bottom: 20px">
                    <span style="color: #101010; font-weight: 600; font-size: 18px">当前列表是空哦~</span>
                    <div style="color: #8F959E; padding-top: 15px">点击右上角的创建，添加您的知识库吧</div>
                </div>

                <!-- 知识库名称 -->
                <el-table-column prop="name" label="知识库名称" width="300"></el-table-column>

                <!-- 知识库描述 -->
                <el-table-column prop="describe" label="知识库描述" :show-overflow-tooltip="true"></el-table-column>

                <!-- 关联微信 -->
                <!-- <el-table-column prop="relevanceWeChat" label="关联微信">
                    <template slot-scope="scope">
                    <template>
                        <div class="tagVue flex_center align_center">
                            <el-tag type="info" class="elTag">啦啦啦啦啦啦</el-tag>
                            <el-tag type="info" class="elTag">啦啦啦啦啦啦</el-tag>
                        </div>
                    </template>
                </el-table-column> -->

                <!-- 操作 编辑 删除 -->
                <el-table-column label="操作" width="220">
                    <template slot-scope="scope">
                        <div class="flex_center">
                            <el-button size="mini" type="manage" @click="$jump('manage', {id: scope.row.id})">管理</el-button>
                            <el-button size="mini" type="edit" @click="handleEdit(scope.row)">编辑</el-button>
                            <el-button size="mini" type="delete" @click="handleDelete(scope.row.id)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 分页 -->
        <div class="paginationVue flex_center align_center">
            <el-pagination background layout="total, prev, pager, next" :page-size="pageSize" :total="total" @current-change="handleCurrentChange"></el-pagination>
        </div>

        <!-- 创建知识库 -->
        <el-dialog title="创建知识库" :visible.sync="dialogTableVisible" :show-close="false" :center="true" top="100px" @close="close">
            <div class="dialogVue">
                <div class="inpVue">
                    <div class="label">知识库名称</div>
                    <el-input v-model="repositoryName" placeholder="请输入知识库名称"></el-input>
                </div>

                <div class="inpVue">
                    <div class="label">知识库描述</div>
                    <textarea placeholder="输入知识库描述,生成效果会更好" v-model="repositoryDescription"></textarea>
                </div>

                <!-- <div class="inpVue">
                    <div class="label">关联微信号</div>
                    <el-select v-model="weChatId" placeholder="请选择微信" filterable  style="width: 100%;" @change="handleChange">
                        <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
                            <span style="color: #000;">{{ item.name }}</span>
                        </el-option>
                    </el-select>  
                </div> -->

                <div class="flex_between margin30">
                    <div class="cancel" @click="close">取消</div>
                    <div class="create" @click="submitFun">
                        <span class="el-icon-loading" v-if="loading"></span>
                        <span v-else>确定</span>
                    </div>
                </div>
            </div>
        </el-dialog>

        <!-- 编辑知识库 -->
        <el-dialog title="创建知识库" :visible.sync="dialogTableVisible20" :show-close="false" :center="true" top="100px" @close="close">
            <div class="dialogVue">
                <div class="inpVue">
                    <div class="label">知识库名称</div>
                    <el-input v-model="repositoryName" placeholder="请输入知识库名称"></el-input>
                </div>
                <div class="inpVue">
                    <div class="label">知识库描述</div>
                    <textarea placeholder="输入知识库描述,生成效果会更好" v-model="repositoryDescription"></textarea>
                </div>
                <!-- <div class="inpVue">
                    <div class="label">关联微信号</div>
                    <div class="tagVue flex">
                        <el-tag type="info" class="elTag">标签二</el-tag>
                    </div>
                </div> -->
                <div class="flex_between margin30">
                    <div class="cancel" @click="close">取消</div>
                    <div class="create" @click="editFun">
                        <span class="el-icon-loading" v-if="loading"></span>
                        <span v-else>确定</span>
                    </div>
                </div>
            </div>
        </el-dialog>

        <!-- 什么是知识库 -->
        <whatVue :dialogTableVisible10="dialogTableVisible10" @close="close"></whatVue>
    </div>
</template>

<script>
import whatVue from './components/what.vue'
import { toast } from '@/common/tools'
export default {
    components: {
        whatVue,
    },
    data(){
        return {
            repositoryName: "",
            repositoryDescription: "",
            dialogTableVisible: false,
            dialogTableVisible10: false,
            dialogTableVisible20: false,
            options: [],
            pageNum: 1,
            pageSize: 15,
            total: 0,
            list: [],
            id: "",
            weChatId: "",
            loading: false,
        }
    },
    created(){
        this.getList()
    },
    methods: {
        // 删除知识库
        handleDelete(id){
            this.$alert('确认删除该知识库?', '删除知识库', {
                confirmButtonText: '确定',
                confirmButtonClass: 'confirmButtonClass',
                cancelButtonText: '取消',
                showCancelButton: true,
                showClose: false,
                callback: action => {
                    console.log(action)
                    if(action==="confirm") {
                        this.$post("/front/dataset/deleteDataset", {
                            ids: id
                        }).then(res=>{
                            if(res.data.code===0) {
                                toast(0, "删除成功")
                                this.getList()
                            }  
                        })
                    }
                }
            });
        },

        close(){
            this.id = ""
            this.repositoryName = ""
            this.repositoryDescription = ""
            this.dialogTableVisible = false
            this.dialogTableVisible20 = false
            this.dialogTableVisible10 = false
        },

        // 获取知识库列表
        getList(){
            this.$post("/front/dataset/datasetList", {
                pageNum: this.pageNum,
                pageSize: this.pageSize
            }).then(res=>{
                console.log(res)
                this.list = res.data.obj.list
                this.total = res.data.obj.total
            })
        },

        // 微信列表
        getWeChatList(){
            this.$post("/front/userWechat/wechatList", {
                pageNum: 1,
                pageSize: 1000
            }).then(res=>{
                if(res.data.code===0) {
                    console.log(res)
                    this.options = res.data.obj.list
                }
            })
        },

        // 创建知识库
        submitFun(){
            if(!this.repositoryName) return toast(1, "请输入知识库名称")
            if(!this.repositoryDescription) return toast(1, "请输入知识库描述")
            if(this.loading) return
            this.loading = true
            this.$post("/front/dataset/addDataset", {
                name: this.repositoryName,
                describe: this.repositoryDescription
            }).then(res=>{
                this.loading = false
                if(res.data.code===0) {
                    this.close()
                    toast(0, "知识库创建成功")
                    this.getList()
                }else{
                    toast(1, res.data.msg)
                }
            })
        },

        // 编辑知识库
        handleEdit(item){
            this.repositoryName = item.name
            this.repositoryDescription = item.describe
            this.id = item.id
            this.dialogTableVisible20 = true
        },

        editFun(){
            if(!this.repositoryName) return toast(1, "请输入知识库名称")
            if(!this.repositoryDescription) return toast(1, "请输入知识库描述")
            if(this.loading) return
            this.loading = true
            this.$post("/front/dataset/updateDataset", {
                id: this.id,
                name: this.repositoryName,
                describe: this.repositoryDescription
            }).then(res=>{
                this.loading = false
                if(res.data.code===0) {
                    this.close()
                    toast(0, "编辑成功")
                    this.getList()
                }else{
                    toast(1, res.data.msg)
                }
            })
        },

        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.pageNum = val
            this.getList()
        }
    }
}
</script>

<style lang="less" scoped>
:deep(.el-dialog) {
    width: 500px;
    height: auto;
    border-radius: 10px;
}
:deep(.el-dialog__title) {
    color: #111;
    font-size: 16px;
    font-weight: 600;
}
:deep(.el-dialog__header) {
    padding: 20px 0 0;
}
:deep(.el-dialog__body) {
    padding: 30px;
}
:deep(.el-input__inner) {
    height: 50px;
}
:deep(.center-table td), 
:deep(.center-table th) {
  text-align: center !important;
}
:deep(.center-table th) {
  background: #f5f5f5 !important;
}
:deep(.el-button--delete) {
    background: #d81e06;
    span {
        color: #fff !important;
    }
}
:deep(.el-button--manage) {
    background: #0147fd;
    span {
        color: #fff !important;
    }
}
:deep(.el-button--edit) {
    background: #AAA;
    span {
        color: #fff !important;
    }
}

.repositoryPage {
    width: 100%;
    height: 100%;
    padding: 20px 20px 0;
    .titleVue {
        width: 100%;
        .title {
            color: #111;
            font-size: 18px;
            font-weight: 600;
        }
        .what {
            color: #81868E; 
            cursor: pointer; 
            padding-right: 20px;
        }
        .what:hover {
            color: #0147fd;
            text-decoration: underline;
        }
        .createBtn {
            width: 80px;
            height: 36px;
            background: #0147fd;
            border-radius: 4px;
            text-align: center;
            line-height: 36px;
            color: #fff;
            font-size: 15px;
            font-weight: 600;
            cursor: pointer;
        }
    }
    .scrollContent {
        width: 100%;
        margin-top: 10px;
        .tagVue {
            overflow: hidden;
            .elTag {
                margin-right: 10px;
            }
        }
    }
    .paginationVue {
        width: 100%;
        height: 60px;
    }
}
.dialogVue {
    width: 100%;
    border-radius: 5px;
    .inpVue {
        width: 100%;
        margin-top: 20px;
        .label {
            color: #333;
            font-size: 14px;
            margin-bottom: 12px;
        }
        textarea {
            width: 100%;
            height: 220px;
            border: #DCDFE6 1px solid;
            display: block;
            box-sizing: border-box;
            resize: none;
            border-radius: 4px;
            margin-top: 10px;
            font-family: PingFangSC, PingFang SC;
            padding: 10px;
            font-size: 13px;
            color: #333;
            line-height: 1.4;
        }
        ::-webkit-input-placeholder { /* WebKit browsers */
            color: #AAA;
            font-size: 13px;
            font-family: PingFangSC, PingFang SC;
        }
        :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
            color: #AAA;
            font-size: 13px;
            font-family: PingFangSC, PingFang SC;
        }
        ::-moz-placeholder { /* Mozilla Firefox 19+ */
            color: #AAA;
            font-size: 13px;
            font-family: PingFangSC, PingFang SC;
        }
        :-ms-input-placeholder { /* Internet Explorer 10+ */
            color: #AAA;
            font-size: 13px;
            font-family: PingFangSC, PingFang SC;
        }
        .tagVue {
            margin-top: 10px;
            .elTag {
                margin: 0 5px;
            }
        }
    }
    .cancel, .create {
        width: 200px;
        height: 42px;
        border-radius: 8px;
        border: none;
        cursor: pointer;
        text-align: center;
        line-height: 42px;
    }
    .cancel {
        background: transparent;
        border: 1px solid #DCDFE6;
        color: #999;
        line-height: 40px;
    }
    .create {
        background: #0147fd;
        span {
            color: #fff;
        }
    }
}
</style>